<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title + '部门'"
      :width="modalWidth"
      @cancel="handleClose"
      :destroyOnClose="true"
      @ok="handleOk"
      :visible="visible"
      :confirmLoading="confirmLoading"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="handleOk" type="primary">确认</a-button>
        </div>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form-model ref="ruleForm" :model="model" :rules="rules">
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="上级部门"
            hasFeedback
          >
            <a-tree-select
              v-model.trim="model.parentId"
              style="width: 100%"
              :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
              :tree-data="treeData"
              :allowClear="true"
              :replaceFields="replaceFields"
              placeholder="请选择上级部门"
              tree-default-expand-all
            ></a-tree-select>
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="部门名称"
            prop="departmentName"
            hasFeedback
          >
            <a-input
              placeholder="请输入部门名称"
              v-model.trim="model.departmentName"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="地址"
            hasFeedback
          >
            <a-input placeholder="请输入地址" v-model.trim="model.address" />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="电话"
            hasFeedback
          >
            <a-input placeholder="请输入电话" v-model.trim="model.telephone" />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="部门编号"
            hasFeedback
          >
            <a-input
              :disabled="title == '编辑' ? true : false"
              placeholder="请输入部门编号"
              v-model.trim="model.departmentNumber"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="备注"
            hasFeedback
          >
            <a-input placeholder="请输入备注" v-model.trim="model.note" />
          </a-form-model-item>
        </a-form-model>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import api from "@/api/apiServer";
export default {
  data() {
    return {
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 652, // 抽屉宽度
      model: {
        parentId: null,
      },
      treeData: [],
      replaceFields: {
        title: "departmentName",
        key: "id",
        value: "id",
        children: "childList",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
    };
  },
  computed: {
    rules() {
      return {
        departmentName: [{ required: true, message: "请输入部门名称" }],
      };
    },
  },
  methods: {
    // 新增
    modalAdd() {
      this.modalEdit({});
    },
    // 编辑
    modalEdit(record) {
      this.resetScreenSize();
      this.getTree();
      this.visible = true;
      this.model = Object.assign({}, record);
      if (this.model.parentId === 0) {
        this.model.parentId = null;
      }
    },
    async getTree() {
      const res = await api.DepartmentTree();
      this.treeData = res.data;
    },
    async submitVulFun() {
      this.confirmLoading = true;
      let res;
      if (this.model.parentId == null) {
        this.model.parentId = 0;
      }
      if (!this.model.id) {
        res = await api.DepartmentAdd(this.model);
      } else {
        res = await api.DepartmentEdit(this.model);
      }
      if (this.model.parentId === 0) {
        this.model.parentId = null;
      }
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        this.model = Object.assign({}, this.model);

        return;
      }
      this.$message.success(res.message);
      this.confirmLoading = false;
      this.$emit("getTree");
      this.$emit("ok");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.submitVulFun();
        } else {
          return false;
        }
      });
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.model = Object.assign({});
      this.$refs.ruleForm.resetFields();
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 652;
      }
    },
  },
};
</script>


